import {_decorator, Component, Node, resources, TextAsset, Label, UITransform, Widget} from 'cc';

const {ccclass, property} = _decorator;

//文本滚动
@ccclass('TextScrollScript')
export class TextScrollScript extends Component {
    //标题
    title!: string
    //内容
    context!: string

    @property(Node)
    titleLabel!: Node
    @property(Node)
    contextLabel!: Node
    @property(Node)
    contextView!: Node

    onLoad() {
        const that = this;
        resources.load("滕王阁序", function (err, data: TextAsset) {
            if (err) {
                console.error(err);
                return
            }
            //文本标题
            that.title = data.name
            //文本内容
            that.context = data.text
        })
    }

    start() {
        this.titleLabel.getComponent(Label).string = this.title
        let size = 22
        let count = this.context.length / size
        let flag = this.context.length % size == 0;
        count = flag ? count : count + 1
        let newStr = ''
        for (let i = 0; i < count; i++) {
            let min = i * size
            let max = size * i + size
            if (size * i >= this.context.length) max = this.context.length

            newStr += this.context.substring(min, max) + "\n";
        }
        this.contextLabel.getComponent(Label).string = newStr
        this.contextLabel.getComponent(Widget).left = 20
        //文本内容高度 context高度 匹配文本内容高度
        this.contextView.getComponent(UITransform).height = 32*count
    }

    update(deltaTime: number) {

    }
}


